'ODM Tools
'Copyright (c) 2007, Utah State University
'All rights reserved.
'Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
'*           Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
'*           Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
'*           Neither the name of the Utah State University nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
'THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Module modDatabase

    Public Const db_val_ValCensorCode_lt As String = "'lt'"
    Public Const db_val_UnitsTimeSupport_MilliSecond As String = "millisecond"
    Public Const db_val_UnitsTimeSupport_Second As String = "second"
    Public Const db_val_UnitsTimeSupport_Minute As String = "minute"
    Public Const db_val_UnitsTimeSupport_Hour As String = "hour"
    Public Const db_val_UnitsTimeSupport_Day As String = "day"

#Region " Controlled Vocabulary Table Definitions "

    'values
    Public Const db_val_VTCVTerm_DerivedValue As String = "Derived Value" 'the term for a 'Derived Value' from the ValueTypeCV table
    Public Const db_val_DTCVTerm_Maximum As String = "Maximum" 'the term for the Maximum type from the DataTypeCV table
    Public Const db_val_DTCVTerm_Minimum As String = "Minimum" 'the term for the Minimum type from the DataTypeCV table
    Public Const db_val_DTCVTerm_Average As String = "Average" 'the term for the Average type from the DataTypeCV table
    'Public Const db_val_CCCVTerm_NULL As String = "nc" 'the term for a NULL Censor Code value from the CensorCodeCV table

#End Region

#Region " Database Functions "

    Public Function TestDBConnection(ByRef e_DBSettings As clsConnectionSettings, Optional ByRef ErrorMessage As String = "NONE") As Boolean
        'Used to test a databse connection
        'Inputs:  Settings -> A ConnectionSettings instance used to create a connection to a database
        'Outputs: TestDBConnection -> Returns True if the test was successful, otherwise returns False
        
        ' TODO: Implement some kind of database connection test mechanism
        Return True

        'Create a new connection
        'Dim testConn As New OleDb.OleDbConnection(e_DBSettings.ConnectionString) 'Temporary connection settings to test
        'Dim sql As String 'SQL command to test DB with
        'If e_DBSettings.DBName = "" Or e_DBSettings.ServerAddress = "" Then
        '    Return False
        'Else
        '    Try

        '        testConn.Open()

        '        'Create an sql command that accesses all tables and a field within the series catalog table
        '        sql = "SELECT MAX(" & db_fld_ODMVersionNumber & ") AS Max_Version FROM " & db_tbl_ODMVersion

        '        'Test the connection
        '        Dim temp As New OleDb.OleDbCommand(sql, testConn) 'Command to test the DB with
        '        Dim maxVersion As String = temp.ExecuteScalar
        '        If maxVersion <> db_const_Version Then
        '            Throw New Exception("Database Version Incorrect." & vbCrLf & _
        '            "This is an ODM Version " & maxVersion & " database." & vbCrLf & _
        '            "This application requires an ODM Version " & db_const_Version & " database.")
        '        End If

        '        testConn.Close()
        '        testConn.Dispose()
        '    Catch ex As Exception
        '        'If the connection timed out, increment the timeout setting, return the results of another test, else return false
        '        If ex.Message.Contains("Timeout expired") Then
        '            If e_DBSettings.IncrementTimeout() Then
        '                Return TestDBConnection(e_DBSettings)
        '            End If
        '        ElseIf ex.Message.Contains("SQL Server does not exist") Then
        '            ShowError("Server Address Incorrect.", ex)
        '            Return False
        '        ElseIf ex.Message.Contains("Cannot open database") Then
        '            ShowError("Database Name Incorrect.", ex)
        '            Return False
        '        ElseIf ex.Message.Contains("Login failed for user") Then
        '            ShowError("Username or Password Incorrect.", ex)
        '            Return False
        '        ElseIf ErrorMessage = "NONE" Then
        '            ShowError("Unable to connect to Database", ex)
        '            Return False
        '        Else
        '            ErrorMessage = ex.Message
        '            Return False
        '        End If
        '    End Try
        '    Return True
        'End If
        'No Errors
    End Function

#End Region

End Module
